//
//  PPAlbumScrollView.m
//  pickpic
//
//  Created by 胡文广 on 2017/4/29.
//  Copyright © 2017年 MicroDream. All rights reserved.
//

#import "PPAlbumScrollView.h"
#import "PPTemplateCoverFrame.h"
#import "PPHomeAlbumModel.h"
#import "PPTemplateInnerModel.h"
#import "UIImage+PPImage.h"
#import "PPProgressView.h"
#import "PPPictureLoadingProgressView.h"
#define kEmploy @"Employ"

#define kEdit @"kEdit"
@interface PPAlbumScrollView()<UIScrollViewDelegate,UIGestureRecognizerDelegate>



//@property (nonatomic,strong) UIImageView *albumImageView;

@property (nonatomic,strong,readwrite) UIScrollView *scrollView;

@property (nonatomic,strong) CAShapeLayer *shapeLayer;
@property (nonatomic,strong) CAShapeLayer *shapeCoverLayer;
@property (nonatomic,strong) UIBezierPath *recPath;
@property (nonatomic,strong) UIBezierPath *recCoverPath;
@property (nonatomic,assign) CGPoint beginPoint;

@property (nonatomic,assign) BOOL isEdit;

@property (nonatomic,strong) UIPanGestureRecognizer *panRec;
@property (nonatomic,strong) UISwipeGestureRecognizer *swipRec;

@property (nonatomic,strong) PPProgressView *loop;

@property (nonatomic,strong) UIImageView *failImage;

//@property (nonatomic,strong) PPPictureLoadingProgressView *loopView;

@property (nonatomic,assign) BOOL isShowCoverLineLayer;
@end
@implementation PPAlbumScrollView
- (void)setIsOpenEdit:(BOOL)isOpenEdit{
    
    
    self.isEdit = isOpenEdit;
    
}
- (UIImageView *)failImage{

    if (!_failImage) {
        
        _failImage = [[UIImageView alloc] init];
        _failImage.image = [UIImage imageNamed:@"preview_image_failure"];
    }
    return _failImage;
}
- (PPProgressView *)loop{

    if (!_loop) {
        _loop = [[PPProgressView alloc] init];
//        _loop.frame = CGRectMake(0,0 , 80, 80);
//        _loop.center = CGPointMake(self.width/2, self.height/2);
        _loop.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
        _loop.hidden = YES;

    }
    return _loop;
}

- (UIScrollView *)scrollView{

    if (!_scrollView) {
        
        _scrollView = [[UIScrollView alloc] init];
        
        _scrollView.maximumZoomScale = 3.0;
        _scrollView.minimumZoomScale = 1;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.delegate = self;
        _scrollView.bounces = NO;
        _scrollView.scrollEnabled = NO;
        [_scrollView addSubview:self.albumImageView];
        
        UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapGesture:)];
        //        tapGesture.numberOfTapsRequired=2;
        [_scrollView addGestureRecognizer:tapGesture];
        _scrollView.backgroundColor = CLEAR;
        
    }
    return _scrollView;
}
- (void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self name:nil object:nil];
//    [self removeObserver:self forKeyPath:@"isEdit"];
}
- (UIImageView *)albumImageView{

    if (!_albumImageView) {
        
        _albumImageView = [[UIImageView alloc] init];
        _albumImageView.userInteractionEnabled = YES;
//        _albumImageView.contentMode =UIViewContentModeScaleAspectFit;
        //超出容器范围的切除掉
//        _albumImageView.clipsToBounds = YES;

        _albumImageView.backgroundColor = CLEAR;
        _albumImageView.backgroundColor = RED;
        self.panRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
        self.panRec.delegate = self;
        [_albumImageView addGestureRecognizer:self.panRec];

        self.swipRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
        self.swipRec .delegate = self;
        
        //设置轻扫的方向
        self.swipRec.direction = UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight;
        [_albumImageView addGestureRecognizer:self.swipRec];
        
    }
    return _albumImageView;
}
- (instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = CLEAR;
        self.scrollView.backgroundColor = CLEAR;
        [self addSubview:self.scrollView];
        
    }
    return self;
}

//轻扫手势触发方法
-(void)swipeGesture:(id)sender{
    UISwipeGestureRecognizer *swipe = sender;
    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft){
        
        PPLog(@"左扫");
    }
    if (swipe.direction == UISwipeGestureRecognizerDirectionRight){
        PPLog(@"右扫");
        
    }
}
- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer{
    
    
    [self.swipRec requireGestureRecognizerToFail:self.panRec];
    [self.swipRec requireGestureRecognizerToFail:self.panRec];
    
//    [self.self.panRec requireGestureRecognizerToFail:self.self.swipRec];
//    [self.self.panRec requireGestureRecognizerToFail:self.self.swipRec];

}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{

    
//    PPLog(@"第一个%@ ---第二个 %@",gestureRecognizer,otherGestureRecognizer);
//    
//    if (!self.isEdit && [gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
//        
//        self.scrollView.scrollEnabled = NO;
//        
//        return NO;
//    }
//    self.scrollView.scrollEnabled = YES;
    
    
    return YES;
}
//关闭编辑状态
- (void)unEdit{
            _albumImageView.userInteractionEnabled = NO;
            _scrollView.scrollEnabled = NO;
            _scrollView.maximumZoomScale = 1;
            _scrollView.minimumZoomScale = 1;
}


#pragma mark -监听编辑状态
- (void)handleTapGesture:(UITapGestureRecognizer *)tap{

    [[UIApplication sharedApplication].keyWindow endEditing:YES];;
    
    PPLog(@"点击开始编辑 - %d",self.isEdit);
    
    self.isEdit = YES;
    
    if (self.isEdit) {
//        编辑状态
        if ([self.delegate respondsToSelector:@selector(albumImageBeginEditorView:)]) {
            [self.delegate albumImageBeginEditorView:self];
        }
    
        self.albumImageView.userInteractionEnabled = YES;
        [self showShapelayer];
    
        _scrollView.scrollEnabled = YES;
        _scrollView.maximumZoomScale = 3.0;
        _scrollView.minimumZoomScale = 1;
    
        BOOL isEdit = [[[NSUserDefaults standardUserDefaults] objectForKey:@"editOperation"] boolValue];
        if (!isEdit) {
            [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"editOperation"];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }


        [[NSNotificationCenter defaultCenter] postNotificationName:PPNotice_isEditImage object:@(YES)];
    

    
    
    }
//        else{
//        if ([self.delegate respondsToSelector:@selector(albumImageEndEditorView:)]) {
//            [self.delegate albumImageEndEditorView:self];
//        }
//
//        [self hiddenBezierPath];
//        _scrollView.scrollEnabled = NO;
//        _scrollView.maximumZoomScale = 1;
//        _scrollView.minimumZoomScale = 1;
//    
//        
//        
//        if (self.innerModel) {
//            
//            [[NSNotificationCenter defaultCenter] postNotificationName:PPNotice_ChangeAlbumsPic object:@{kCutPicKey:@(self.tag),kCutPicValue:[UIImage imageFromView:self]}];
//        }else{
//            if ([self.delegate respondsToSelector:@selector(albumCoverViewEndEditView: cutFrame:)]) {
//                [self.delegate albumCoverViewEndEditView:self cutFrame:self.cutRect];
//            }
//        }
        
        
        

//    }

//    [[NSNotificationCenter defaultCenter] postNotificationName:PPNotice_isEditImage object:@(self.isEdit)];
    
}
#pragma mark -取消编辑

- (void)cancelEdit{

    self.isEdit = NO;
    
    [self.recCoverPath removeAllPoints];
    [self.shapeCoverLayer removeFromSuperlayer];
    
    [self hiddenBezierPath];
    
//    self.albumImageView.userInteractionEnabled = NO;
    
    [self unEdit];
//    _scrollView.scrollEnabled = NO;
//    _scrollView.maximumZoomScale = 1;
//    _scrollView.minimumZoomScale = 1;
    
    
//    if ([self.delegate respondsToSelector:@selector(albumImageEndEditorView:)]) {
//        [self.delegate albumImageEndEditorView:self];
//    }
//    
//    self.coverF = _coverF;
//    
//    if (self.innerModel) {
//        
//        [[NSNotificationCenter defaultCenter] postNotificationName:PPNotice_ChangeAlbumsPic object:@{kCutPicKey:@(self.tag),kCutPicValue:[UIImage imageFromView:self]}];
//    }else{
//        if ([self.delegate respondsToSelector:@selector(albumCoverViewEndEditView: cutFrame:)]) {
//            [self.delegate albumCoverViewEndEditView:self cutFrame:self.cutRect];
//        }
//    }
}
//- (void)noticeEditEnable{
//
//    self.isEdit = NO;
//    
//    [self hiddenBezierPath];
//    _scrollView.scrollEnabled = NO;
//    _scrollView.maximumZoomScale = 1;
//    _scrollView.minimumZoomScale = 1;
//    
//    [[NSNotificationCenter defaultCenter] postNotificationName:PPNotice_isEditImage object:@(self.isEdit)];
//}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if (decelerate){
        dispatch_async(dispatch_get_main_queue(), ^{
            [scrollView setContentOffset:scrollView.contentOffset animated:NO];
        });
    }
}
- (void)setCoverF:(PPTemplateCoverFrame *)coverF{

    _coverF = coverF;
    self.tag = 0;

    if (coverF.orderNum <= 0) {
        [self.scrollView addSubview:self.loop];
    }
    
//    if (coverF.coverModel.coverImage) {
//        
//        CGFloat ratio = (double)coverF.coverModel.coverImage.size.height/(double)coverF.coverModel.coverImage.size.width;
//        CGFloat bigW = coverF.boxWidth;
//        CGFloat bigH = coverF.boxWidth*ratio;
//        
//        if (bigH < self.frame.size.height) {
//            self.albumImageView.bounds = CGRectMake(0, 0, bigW, bigH);
//            
//            self.albumImageView.center = CGPointMake(self.frame.size.width/2, (self.height)/2);
//        }else{//设置长图的frame
//            
//            self.albumImageView.frame = CGRectMake(0, 0, bigW, bigH);
//            self.scrollView.contentSize = CGSizeMake(self.width,bigH);
//        }
//        
//        if (bigH > coverF.boxHeight) {
//            self.scrollView.contentOffset = CGPointMake(0, (bigH - coverF.boxHeight)/2);
//        }else if (bigH < coverF.boxHeight){
//            self.scrollView.contentOffset = CGPointMake(0, (coverF.boxHeight - bigH)/2);
//            
//            self.albumImageView.frame = CGRectMake(0,(coverF.boxHeight - bigH)/2, bigW, bigH);
//            
//        }
//        self.albumImageView.image = coverF.coverModel.coverImage;
//        
//        [self showFrameLayer];
////        [self showCoverShapelayer];
//        
//    }else{
        NSURL *coverURL = [NSURL URLWithString:coverF.coverModel.coverURL];
        
        [[SDImageCache sharedImageCache] queryDiskCacheForKey:coverF.coverModel.coverURL done:^(UIImage *image, SDImageCacheType cacheType) {
            if (image==nil) {
                self.loop.hidden = NO;
            }
            
        }];
        [self.albumImageView sd_setImageWithURL:coverURL placeholderImage:nil options:SDWebImageRetryFailed | SDWebImageLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            self.loop.progressValue = (CGFloat)receivedSize/(CGFloat)expectedSize;
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            if (image!=nil) {
                self.loop.hidden = YES;
                
                if ([coverF.series isEqualToString:@"wave"]) {
                    
                    
//                    if (image.size.height < coverF.boxWidth || image.size.width < coverF.boxWidth) {
//                        CGFloat sum;
//                        if (image.size.height < image.size.width) {
//                            sum  = image.size.height - coverF.boxWidth;
//                        }else{
//                            sum = image.size.width - coverF.boxWidth;
//                        }
//
//                        CGFloat imgW = image.size.width - sum;
//                        CGFloat imgH = image.size.height - sum;
//
//                        self.albumImageView.bounds = CGRectMake(0, 0, imgW, imgH);
//                        self.albumImageView.center = CGPointMake(coverF.boxWidth/2, (coverF.boxHeight)/2);
//                    }else{
                        CGFloat ratio = (double)image.size.height/(double)image.size.width;
                        CGFloat bigW = coverF.boxWidth;
                        CGFloat bigH = coverF.boxWidth*ratio;
                        
                        CGFloat sum = 0;
                        if (bigH < coverF.boxHeight) {
                            sum = (coverF.boxHeight - bigH);
                    
                        }else if(bigW < coverF.boxHeight){
                            sum = (coverF.boxHeight - bigH);
                        }
                        
                        bigH = bigH + sum;
                        bigW = bigW + sum;
                        
                        self.albumImageView.bounds = CGRectMake(0, 0, bigW, bigH);
                        self.albumImageView.center = CGPointMake(coverF.boxWidth/2, (coverF.boxHeight)/2);
//                    }
                    
                   
                        self.backgroundColor = WHITE;
                        self.layer.masksToBounds = YES;
                        self.layer.cornerRadius = coverF.boxWidth/2;

                    if (!self.isShowCoverLineLayer) {
                        self.albumImageView.userInteractionEnabled = NO;
                        
                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                            self.albumImageView.userInteractionEnabled = YES;
                            
                            self.isShowCoverLineLayer = YES;
                        });
                    }
                }else{
                    
                    CGFloat ratio = (double)image.size.height/(double)image.size.width;
                    CGFloat bigW = coverF.boxWidth;
                    CGFloat bigH = coverF.boxWidth*ratio;
                    
                    
//                    if (bigH < coverF.boxHeight) {
                        self.albumImageView.bounds = CGRectMake(0, 0, bigW, bigH);
                        self.albumImageView.center = CGPointMake(coverF.boxWidth/2, (coverF.boxHeight)/2);
                        
                        //                    self.scrollView.contentOffset = CGPointMake(0, (coverF.boxHeight - bigH)/2 + 50);
//                    }else{
//                        //设置长图的frame
//                        //                    if ([coverF.series isEqualToString:@"wave"]) {
//                        self.albumImageView.frame = CGRectMake(0, 0, bigW, bigH);
//                        //                    }
//
//                        self.scrollView.contentOffset = CGPointMake(0, (bigH - coverF.boxHeight)/2);
//
//                    }
                    if (!self.isShowCoverLineLayer) {
                        self.albumImageView.userInteractionEnabled = NO;
                        
                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                            self.albumImageView.userInteractionEnabled = YES;
                            
                            self.isShowCoverLineLayer = YES;
                        });
                    }
                    
                }
            }else{
                //图片下载失败
                [self.loop removeFromSuperview];
                [self addSubview:self.failImage];
            }
            
        }];

//    }
}
- (void)showFrameLayer{

//    BOOL isPath = [[[NSUserDefaults standardUserDefaults] objectForKey:kEmploy] boolValue];
//    
//    if (!isPath) {
//        
//        if (self.coverF) {
//            
//            [self showCoverShapelayer];
//            
//            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kEmploy];
//            [[NSUserDefaults standardUserDefaults] synchronize];
//            
//            [self performSelector:@selector(hiddenCoverBezierPath) withObject:nil afterDelay:2.0f];
//        }else{
//            [self showShapelayer];
//        }
//
//    }
}

- (CAShapeLayer *)shapeLayer{

    if (!_shapeLayer) {
//        self.recPath =[UIBezierPath bezierPathWithRect:self.albumImageView.frame];
        self.recPath =[UIBezierPath bezierPathWithRect:CGRectMake(0, 0, self.albumImageView.width, self.height)];
        _shapeLayer=[CAShapeLayer layer];
//        _shapeLayer.frame = self.albumImageView.frame;
        _shapeLayer.frame = CGRectMake(0, 0, self.albumImageView.width, self.height);
        _shapeLayer.strokeColor = [COLORA(255, 221,23) CGColor];
        _shapeLayer.fillColor = [[UIColor clearColor] CGColor];
        _shapeLayer.lineWidth = 2.0f;
        _shapeLayer.path= self.recPath.CGPath;
    }
    return _shapeLayer;
}
- (CAShapeLayer *)shapeCoverLayer{

    if (!_shapeCoverLayer) {
        self.recCoverPath =[UIBezierPath bezierPathWithRect:CGRectMake(0, 0, self.coverF.boxWidth, self.coverF.boxHeight)];
        _shapeCoverLayer=[CAShapeLayer layer];
        _shapeCoverLayer.frame = CGRectMake(0, 0,self.coverF.boxWidth, self.coverF.boxHeight);
        _shapeCoverLayer.strokeColor = [COLORA(255, 221,23) CGColor];
        _shapeCoverLayer.fillColor = [[UIColor clearColor] CGColor];
        _shapeCoverLayer.lineWidth = 2.0f;
        _shapeCoverLayer.path= self.recCoverPath.CGPath;
    }
    return _shapeCoverLayer;
}
- (void)showCoverShapelayer{
    [self.layer addSublayer:self.shapeCoverLayer];
    
}
- (void)showShapelayer{
    
    [self.layer addSublayer:self.shapeLayer];
    
}
- (void)hiddenBezierPath{
    
    if (self.shapeLayer) {
        [self.recPath removeAllPoints];
        [self.shapeLayer removeFromSuperlayer];
    }
    
}
- (PPPictureLoadingProgressView *)addLoopView{

    PPPictureLoadingProgressView *loopV = [[PPPictureLoadingProgressView alloc] init];
    loopV.backgroundColor = CLEAR;
    loopV.valueColor = [UIColor colorWithHexString:@"#A4A4B3"];
    loopV.trackColor = [UIColor colorWithHexString:@"#F2F2F2"];
    loopV.bounds = CGRectMake(0,0 ,kProgressSize, kProgressSize);
    loopV.center = CGPointMake(self.innerModel.frameWidth/2, self.innerModel.frameHeight/2);

    [self addSubview:loopV];
    return loopV;
}
#pragma mark -设置内页图片
- (void)setInnerModel:(PPTemplateInnerModel *)innerModel{    
    _innerModel = innerModel;
    self.tag = innerModel.picModel.page;
    
    
    PPPictureLoadingProgressView *looView =   [self addLoopView];
    NSURL *innerURL = [NSURL URLWithString:_innerModel.picModel.picURL];
    [[SDImageCache sharedImageCache] queryDiskCacheForKey:_innerModel.picModel.picURL done:^(UIImage *image, SDImageCacheType cacheType) {
        if (image==nil) {
            looView.hidden = NO;
        }
    }];
    [self.albumImageView sd_setImageWithURL:innerURL placeholderImage:nil options:SDWebImageRetryFailed | SDWebImageLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        //设置进度条
        looView.progress = (CGFloat)receivedSize/(CGFloat)expectedSize;
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        if (image!=nil) {
            looView.hidden = YES;
            
            if ([_innerModel.series isEqualToString:@"wave"]) {
//                CGFloat sum;
//                if (image.size.height < image.size.width) {
//                    sum  = image.size.height - _innerModel.frameWidth;
//                }else{
//                    sum = image.size.width - _innerModel.frameWidth;
//                }
//                CGFloat bigW = image.size.width - sum;
//                CGFloat bigH = image.size.height - sum;
                
                CGFloat ratio = (double)image.size.height/(double)image.size.width;
                CGFloat bigW = _innerModel.frameHeight;
                CGFloat bigH = _innerModel.frameHeight*ratio;
                
                CGFloat sum = 0;
                if (bigH < _innerModel.frameHeight) {
                    sum = (_innerModel.frameHeight - bigH);
                    
                }else if(bigW < _innerModel.frameHeight){
                    sum = (_innerModel.frameHeight - bigH);
                }
                
                bigH = bigH + sum;
                bigW = bigW + sum;
                
                self.albumImageView.bounds = CGRectMake(0, 0, bigW, bigH);
                self.albumImageView.center = CGPointMake(_innerModel.frameHeight/2, (_innerModel.frameHeight)/2);
                
                
                self.backgroundColor = WHITE;
                self.layer.masksToBounds = YES;
                self.layer.cornerRadius = _innerModel.frameHeight/2;
//                self.albumImageView.bounds = CGRectMake(0, 0, bigW, bigH);
//                self.albumImageView.center = CGPointMake(_innerModel.frameWidth/2, _innerModel.frameHeight/2);
                
            }else{
                
                CGFloat ratio = (double)image.size.height/(double)image.size.width;
                CGFloat bigW = _innerModel.frameWidth;
                CGFloat bigH = _innerModel.frameWidth*ratio;
                
//                if (bigH < _innerModel.frameHeight) {
                    self.albumImageView.bounds = CGRectMake(0, 0, bigW, bigH);
                    self.albumImageView.center = CGPointMake(_innerModel.frameWidth/2, _innerModel.frameHeight/2);
                    
                    
//                }else{//设置长图的frame
//                    self.albumImageView.frame = CGRectMake(0, 0, bigW, bigH);
//                    self.scrollView.contentSize = CGSizeMake(self.width,bigH);
                
                    //                if ([_innerModel.series isEqualToString:@"wave"]) {
                    //                    CGFloat wh = bigH > bigW?bigH:bigW;
                    //                    self.backgroundColor = WHITE;
                    //                    self.layer.masksToBounds = YES;
                    //                    self.layer.cornerRadius = wh/2;
                    //                }
//                }
 
            }

        }else{
            //图片下载失败
            [looView removeFromSuperview];
            [self addSubview:self.failImage];
        }
        
    }];
    

    
    
//    self.albumImageView.frame = CGRectMake(0, 0,innerModel.frameWidth, innerModel.frameHeight);
//    self.albumImageView.contentMode = UIViewContentModeScaleAspectFit;
//    _scrollView.contentSize = self.albumImageView.frame.size;
    
}

- (void)layoutSubviews{

    [super layoutSubviews];
    
    self.loop.frame = CGRectMake(self.width/2-40,self.height/2-40,80,80);
//    self.loopView.bounds = CGRectMake(0,0 ,kProgressSize, kProgressSize);
//    self.loopView.center = CGPointMake(self.centerX, self.centerY/2 - 64/2);
    self.scrollView.frame = CGRectMake(0, 0, self.width, self.height);
    self.failImage.frame = self.frame;
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    
//    if (!self.isEdit) {
//        return nil;
//    }
    
    return _albumImageView;
    
}
-(void)scrollViewDidZoom:(UIScrollView *)scrollView{
    
    if (!self.isEdit) {
        return;
    }
    CGFloat photoY = (self.albumImageView.frame.size.height-self.albumImageView.frame.size.height)/2;
    CGRect photoF = self.albumImageView.frame;
    
    if (photoY>0) {
        photoF.origin.y = photoY;
    }else{
        photoF.origin.y = 0;
    }
    
    self.albumImageView.frame = photoF;
    
//    if (self.shapeLayer) {
//        [self.shapeLayer removeFromSuperlayer];
//        self.shapeLayer = nil;
//    }
    
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
    if (!self.isEdit) {
        return;
    }
    //如果结束缩放后scale为1时，跟原来的宽高会有些轻微的出入，导致无法滑动，需要将其调整为原来的宽度
    if (scale == 1.0) {
        CGSize tempSize = scrollView.contentSize;
        tempSize.width = self.width;
        scrollView.contentSize = tempSize;
        CGRect tempF = view.frame;
        tempF.size.width = self.width;
        view.frame = tempF;
    }
    
}
- (void)panView:(UIPanGestureRecognizer *)panGestureRecognizer{
    if (!self.isEdit) {
        return;
    }
    
    UIView *view = panGestureRecognizer.view;
    
    if (panGestureRecognizer.state == UIGestureRecognizerStateBegan || panGestureRecognizer.state == UIGestureRecognizerStateChanged)
        
    {
        
        CGPoint translation = [panGestureRecognizer translationInView:view.superview];
        
        [view setCenter:(CGPoint){view.center.x + translation.x, view.center.y + translation.y}];
        
        [panGestureRecognizer setTranslation:CGPointZero inView:view.superview];      }
    
}

- (UIImage *)cutImage{

    return  [UIImage imageFromView:self];
}

@end
